<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/15
 * Time: 16:46
 */

namespace app\admin\controller;


use app\common\controller\ReturnCode;
use think\Controller;
use think\Request;
use think\Session;

class Index extends Controller
{
    private $accout;            //账号
    private $pwd;              //密码
    private $aid;               //admin_id
    private $token;             //token
    private $hasToken;          //token 认证
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this -> accout = $request -> param('accout');
        $this -> pwd = $request -> param('pwd');
        $this -> aid = $request -> param('aid');
        $this -> token = $request -> param('token');

        $this -> hasToken = hasToken($this -> aid , $this -> token);
    }
    //注册
    public function reg(){
        if(empty($this -> accout) || empty($this -> pwd)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
//        $data = ['admin_id' => uuid(),'accout' => $this -> accout, 'pwd' => md5($this -> pwd),'a_name' => '管理员'.$this -> accout];
        $where = ['accout' => $this -> accout, 'pwd' => md5($this -> pwd)];
        $has = db('admin')->where($where)->find();
        if($has){
            return json(['status' => 23,'msg'=>ReturnCode::$return_code[23]]);
        }

        $data = ['accout' => $this -> accout, 'pwd' => md5($this -> pwd),'a_name' => '管理员'.$this -> accout];

        $res = db('admin')->insert($data);
        if ($res){
            return json(['status' => 0,'msg'=>ReturnCode::$return_code[0]]);
        }
        return json(['status' => 1,'msg'=>ReturnCode::$return_code[1]]);
    }
    //登录
    public function  dologin(){
        if(empty($this -> accout) || empty($this -> pwd)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
       $where = ['accout'=>$this -> accout,'pwd'=>md5($this -> pwd)];
       $token = setToken();
       $has = db('admin')->where($where)->find();

       if($has){
           $data = [
               'token'=>$token,
               'last_time' => $has['login_time'],
               'last_ip' => $has['login_ip'],
               'login_time' => date('Y-m-d H:i:s',time()),
               'login_ip'=>$_SERVER['REMOTE_ADDR']
           ];
           $where2 = ['admin_id' => $has['admin_id']];
           $res = db('admin')->where($where2)->update($data);
           if($res){
               $field = "admin_id,a_name,accout,token,last_time,last_ip";
               $data = db('admin')->field($field)->where($where2)->find();
               Session::set('admin_'.$has['admin_id'],['token'=>$token,'time'=>time()]);
//               Session::set('admin_'.$has['admin_id'],$token,7200);
               return json(['status' => 0,'msg'=>ReturnCode::$return_code[0],'data'=>$data]);
           }
           return json(['status' => 1,'msg'=>ReturnCode::$return_code[1]]);
       }
       return json(['status' => 10,'msg'=>ReturnCode::$return_code[10]]);
    }
    //退出
    public function outlogin(){
//        dump(isset($this->aid));
//        dump($this -> token);
//        die;
        if(!isset($this -> aid) || !isset($this -> token)){
            return json(['status' => 2,'msg'=>ReturnCode::$return_code[2]]);
        }
//        dump($this -> hasToken);
//        die;
        if($this -> hasToken){
            dump(Session::get());
//            Session::clear('admin_'.$this->aid);
            return json(['status' => 0,'msg'=>ReturnCode::$return_code[0]]);
        }
        return json(['status' => 3,'msg'=>ReturnCode::$return_code[3]]);
    }
    //添加店信息
//    public function shopadd(){
//
//    }
    //修改店信息
    public function shopup(Request $request){
        $shopName = $request -> param('shop_name');
        $shopPhone = $request -> param('shop_phone');
        $shopAddress = $request -> param('shop_address');
        $shopLon = $request -> param('shop_lon');
        $shopLat = $request -> param('shop_lat');

        if(!isset($this -> aid) || empty($this -> token)){
            return json(['status' => 2,'msg' => ReturnCode::$return_code[2]]);
        }
        if($this ->hasToken){
            $where = ['admin_id' => $this -> aid];
            $data = [
                'shopName' => $shopName,
                'shopPhone' => $shopPhone,
                'shopAddress' => $shopAddress,
                'shopLon' => $shopLon,
                'shopLat'=> $shopLat,
                ];
            $res = db('admin')->where($where)->update($data);
            if($res){
                return json(['status' => 0,'msg' => ReturnCode::$return_code[0]]);
            }
            return json(['status' => 1,'msg'=> ReturnCode::$return_code[1]]);
        }
        return json(['status'=>3,'msg'=>ReturnCode::$return_code[3]]);
    }

}